{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "90cd3ded",
   "metadata": {},
   "source": [
    "# Cassandra \n",
    "\n",
    ">[Apache Cassandra®](https://cassandra.apache.org) is a `NoSQL`, row-oriented, highly scalable and highly available database, well suited for storing large amounts of data.\n",
    "\n",
    ">`Cassandra` is a good choice for storing chat message history because it is easy to scale and can handle a large number of writes.\n",
    "\n",
    "This notebook goes over how to use Cassandra to store chat message history.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f507f58b-bf22-4a48-8daf-68d869bcd1ba",
   "metadata": {},
   "source": [
    "## Setting up\n",
    "\n",
    "To run this notebook you need either a running `Cassandra` cluster or a `DataStax Astra DB` instance running in the cloud (you can get one for free at [datastax.com](https://astra.datastax.com)). Check [cassio.org](https://cassio.org/start_here/) for more information."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d7092199",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install --upgrade --quiet  \"cassio>=0.1.0 langchain-community\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3d97b65",
   "metadata": {},
   "source": [
    "### Set up the database connection parameters and secrets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "163d97f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import getpass\n",
    "\n",
    "database_mode = (input(\"\\n(C)assandra or (A)stra DB? \")).upper()\n",
    "\n",
    "keyspace_name = input(\"\\nKeyspace name? \")\n",
    "\n",
    "if database_mode == \"A\":\n",
    "    ASTRA_DB_APPLICATION_TOKEN = getpass.getpass('\\nAstra DB Token (\"AstraCS:...\") ')\n",
    "    #\n",
    "    ASTRA_DB_SECURE_BUNDLE_PATH = input(\"Full path to your Secure Connect Bundle? \")\n",
    "elif database_mode == \"C\":\n",
    "    CASSANDRA_CONTACT_POINTS = input(\n",
    "        \"Contact points? (comma-separated, empty for localhost) \"\n",
    "    ).strip()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55860b2d",
   "metadata": {},
   "source": [
    "Depending on whether local or cloud-based Astra DB, create the corresponding database connection \"Session\" object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8dff2798",
   "metadata": {},
   "outputs": [],
   "source": [
    "from cassandra.auth import PlainTextAuthProvider\n",
    "from cassandra.cluster import Cluster\n",
    "\n",
    "if database_mode == \"C\":\n",
    "    if CASSANDRA_CONTACT_POINTS:\n",
    "        cluster = Cluster(\n",
    "            [cp.strip() for cp in CASSANDRA_CONTACT_POINTS.split(\",\") if cp.strip()]\n",
    "        )\n",
    "    else:\n",
    "        cluster = Cluster()\n",
    "    session = cluster.connect()\n",
    "elif database_mode == \"A\":\n",
    "    ASTRA_DB_CLIENT_ID = \"token\"\n",
    "    cluster = Cluster(\n",
    "        cloud={\n",
    "            \"secure_connect_bundle\": ASTRA_DB_SECURE_BUNDLE_PATH,\n",
    "        },\n",
    "        auth_provider=PlainTextAuthProvider(\n",
    "            ASTRA_DB_CLIENT_ID,\n",
    "            ASTRA_DB_APPLICATION_TOKEN,\n",
    "        ),\n",
    "    )\n",
    "    session = cluster.connect()\n",
    "else:\n",
    "    raise NotImplementedError"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36c163e8",
   "metadata": {},
   "source": [
    "## Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d15e3302",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.chat_message_histories import (\n",
    "    CassandraChatMessageHistory,\n",
    ")\n",
    "\n",
    "message_history = CassandraChatMessageHistory(\n",
    "    session_id=\"test-session\",\n",
    "    session=session,\n",
    "    keyspace=keyspace_name,\n",
    ")\n",
    "\n",
    "message_history.add_user_message(\"hi!\")\n",
    "\n",
    "message_history.add_ai_message(\"whats up?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64fc465e",
   "metadata": {},
   "outputs": [],
   "source": [
    "message_history.messages"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59902d0f-e9ba-4e3d-a7e0-ce202b9d3c43",
   "metadata": {},
   "source": [
    "#### Attribution statement\n",
    "\n",
    "> Apache Cassandra, Cassandra and Apache are either registered trademarks or trademarks of the [Apache Software Foundation](http://www.apache.org/) in the United States and/or other countries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7efaa51c-e9ee-4dce-80a4-eb9280a0dbe5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
